//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.tuiofx.internal.util;

import javafx.application.Platform;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Window;
import org.tuiofx.internal.gesture.LongPressDetector;

import java.awt.*;

public class Util {
    public static double degreeDev = 13.0;

    public Util() {
    }

    public static double getRotationAngleLocalToScene(Node node) {
        double xx = node.getLocalToSceneTransform().getMxx();
        double xy = node.getLocalToSceneTransform().getMxy();
        double yx = node.getLocalToSceneTransform().getMyx();
        double yy = node.getLocalToSceneTransform().getMyy();
        return Math.atan2(-xy, xx);
    }

    public static double getRotationDegreesLocalToScene(Node node) {
        double xx = node.getLocalToSceneTransform().getMxx();
        double xy = node.getLocalToSceneTransform().getMxy();
        double yx = node.getLocalToSceneTransform().getMyx();
        double yy = node.getLocalToSceneTransform().getMyy();
        return Math.toDegrees(Math.atan2(-xy, xx));
    }

    public static Point2D getTranslationLocalToScene(Node node) {
        double x = node.getLocalToSceneTransform().getTx();
        double y = node.getLocalToSceneTransform().getTy();
        return new Point2D(x, y);
    }

    public static double euclideanDistance(double x, double y) {
        return Math.sqrt(x * x + y * y);
    }

    public static boolean isFirstQuadrant(double degree) {
        return -90.0 + degreeDev <= degree && degree <= 0.0;
    }

    public static boolean isSecondQuadrant(double degree) {
        return 0.0 < degree && degree <= 90.0 - degreeDev;
    }

    public static boolean isThirdQuadrant(double degree) {
        return 90.0 + degreeDev <= degree && degree <= 180.0;
    }

    public static boolean isFourthQuadrant(double degree) {
        return -180.0 <= degree && degree <= -90.0 - degreeDev;
    }

    public static boolean isBesideFirstAndFourthQuadrant(double degree) {
        return -90.0 - degreeDev <= degree && degree <= -90.0 + degreeDev;
    }

    public static boolean isBesideSecondAndThirdQuadrant(double degree) {
        return 90.0 - degreeDev <= degree && degree <= 90.0 + degreeDev;
    }

    public static double getDegreesBetweenTwoPoints(Point2D p1, Point2D p2) {
        double xDiff = p2.getX() - p1.getX();
        double yDiff = p2.getY() - p1.getY();
        return Math.toDegrees(Math.atan2(-yDiff, xDiff));
    }

    public static double getOffsetX(Scene scene) {
        return scene.getWindow().getX() + scene.getX();
    }

    public static double getOffsetY(Scene scene) {
        return scene.getWindow().getY() + scene.getY();
    }

    public static double getOffsetX(Object obj) {
        Scene scene;
        if (obj instanceof Node) {
            scene = ((Node) obj).getScene();
            return scene != null && scene.getWindow() != null ? scene.getX() + scene.getWindow().getX() : 0.0;
        } else if (obj instanceof Scene) {
            scene = (Scene) obj;
            return scene != null && scene.getWindow() != null ? scene.getX() + scene.getWindow().getX() : 0.0;
        } else {
            return obj instanceof Window ? ((Window) obj).getX() : 0.0;
        }
    }

    public static double getOffsetY(Object obj) {
        Scene scene;
        if (obj instanceof Node) {
            scene = ((Node) obj).getScene();
            return scene != null && scene.getWindow() != null ? scene.getY() + scene.getWindow().getY() : 0.0;
        } else if (obj instanceof Scene) {
            scene = (Scene) obj;
            return scene != null && scene.getWindow() != null ? scene.getY() + scene.getWindow().getY() : 0.0;
        } else {
            return obj instanceof Window ? ((Window) obj).getY() : 0.0;
        }
    }

    public static boolean isWithIn(double value, double lowerBound, double upperBound) {
        return value >= lowerBound && value <= upperBound;
    }

    public static void drawDebugCircle(Scene scene, double x, double y) {
        Platform.runLater(() -> {
            Parent startNode = scene.getRoot();
            Pane node = (Pane) startNode;
            node.getChildren().add(new Circle(x, y, 2.0, Color.ORANGE));
        });
    }

    public static int getToolkitMultiClickInterval(Toolkit toolkit) {
        int awtMultiClickInterval;
        try {
            awtMultiClickInterval = (int) toolkit.getDesktopProperty("awt.multiClickInterval");
        } catch (Exception e) {
            awtMultiClickInterval = 200;
            LogUtils.error("获取multiClickInterval失败，使用了默认的");
        }
        return awtMultiClickInterval;
    }
}
